package com.jojocalendar;

import java.util.Calendar;
import com.example.jojocalendar.R;

import android.R.integer;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.Date;
import android.widget.RemoteViews;

public class JoJoWidget extends AppWidgetProvider{	 	 
	 @Override  
     public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {   
         
		 int N = appWidgetIds.length;
	     for(int i=0; i<N; i++){ 
	    	 RemoteViews  views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);  
	         Calendar calendar = Calendar.getInstance();         
	        
	         String dateString = context.getResources().getString(R.string.date);
	         int year  = calendar.get(Calendar.YEAR);
	         int month = calendar.get(Calendar.MONTH);
	         int date  = calendar.get(Calendar.DATE);        
	         views.setTextViewText(R.id.widget_date, String.format(dateString, year, month, date));
	         
	         String[] strThings = context.getResources().getStringArray(R.array.thingArray);
	         int[] idx = CalendarRandom.next(year, month, date);
	         int todo1 = idx[0] % strThings.length;
	         int todo2 = idx[1] % strThings.length;
	         while(todo2 == todo1) 	 todo2 = (todo2+1) % strThings.length;	      	         
	         
	         int nottodo1 = idx[2] % strThings.length;
	         while(nottodo1 == todo1 || nottodo1 == todo2)  nottodo1 = (nottodo1+1) % strThings.length;	         	
	         
	         int nottodo2 = idx[3] % strThings.length;
	         while(nottodo2 == todo1 || nottodo2 == todo2 || nottodo2 == nottodo1) nottodo2 = (nottodo2+1) % strThings.length;
	         	         
	         int nottodo3 = idx[4] % strThings.length;
	         while(nottodo3 == todo1 || nottodo3 == todo2 ||
	         	   nottodo3 == nottodo1 || nottodo3 == nottodo2)  nottodo3 = (nottodo3 + 1) % strThings.length;
	        	         
	         String strTodo    = context.getResources().getString(R.string.todo);
	         String strNotToDo = context.getResources().getString(R.string.nottodo);
	         views.setTextViewText(R.id.widget_todo01, String.format(strTodo, strThings[todo1]));
	         views.setTextViewText(R.id.widget_todo02, String.format(strTodo, strThings[todo2]));
	         views.setTextViewText(R.id.widget_nottodo01, String.format(strNotToDo, strThings[nottodo1]));
	         views.setTextViewText(R.id.widget_nottodo02, String.format(strNotToDo, strThings[nottodo2]));
	         views.setTextViewText(R.id.widget_nottodo03, String.format(strNotToDo, strThings[nottodo3]));
	         
	         Intent detailIntent = new Intent(context, MainActivity.class);
	    	 PendingIntent pending = PendingIntent.getActivity(context, 0, detailIntent, 0);           
	         views.setOnClickPendingIntent(R.id.widget_image_of_today, pending);  
	         
	         appWidgetManager.updateAppWidget(appWidgetIds[i], views); 
	     } 
	        
         Log.v("tag", "update");
     }  
	 
	 @Override
	 public void onReceive(Context context, Intent intent){
		 super.onReceive(context, intent);
	 } 	 
}
